package delphorm.web.controller;



import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import delphorm.entite.personne.Utilisateur;
import delphorm.service.personne.IPersonne;
import delphorm.web.forms.AjouterUtilisateurFormulaire;


public class EditerUtilisateur extends SimpleFormController {
	private static final String[] ALLOWED_PROPERTIES ={
		"nom",
		"prenom",
		"login",
		"password",
		"profession",
		"adressePostale",
		"etat",
		"municipalite",
		"ville",
		"codepostal",
		"pays",
		"adresseElectronique",
		"numeroTelephone",
		"numeroFax",
		"passwordConfirm"
	};
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		super.initBinder(request, binder);
		binder.setAllowedFields(ALLOWED_PROPERTIES);
	}
	
	private Utilisateur ancienUtilisateur;
	private IPersonne servicePersonne;

	public IPersonne getServicePersonne() {
		return servicePersonne;
	}



	public void setServicePersonne(IPersonne servicePersonne) {
		this.servicePersonne = servicePersonne;
	}



	public EditerUtilisateur() {
		super();
		// TODO Auto-generated constructor stub
	}



	protected Object formBackingObject(HttpServletRequest request)
	{
//		récupère le formulaire dans la session
		Object formulaire = request.getSession().getAttribute("formulaire");
		
		if(formulaire == null)
		{
//			récupère l'id de l'utilisateur
			String idutilisateur = request.getParameter("idutilisateur");
			long numero = Long.parseLong(idutilisateur);
			
			Utilisateur utilisateur = getServicePersonne().chercherUtilisateurParId(new Long(numero));
			setAncienUtilisateur(utilisateur);
			formulaire = new AjouterUtilisateurFormulaire(utilisateur);
			
		}
		return formulaire;
	}
	
	protected Map referenceData(HttpServletRequest request, Object object, Errors errors) throws Exception {
		Map ret = new HashMap();
		String[] pays = new String[]{
				"Afghanistan",
				"Albania",
				"Algeria",
				"American Samoa",
				"Andorra",
				"Angola",
				"Anguilla",
				"Antarctica",
				"Antigua And Barbuda",
				"Argentina",
				"Armenia",
				"Aruba",
				"Australia",
				"Austria",
				"Azerbaijan",
				"Bahamas",
				"Bahrain",
				"Bangladesh",
				"Barbados",
				"Belarus",
				"Belgium",
				"Belize",
				"Benin",
				"Bermuda",
				"Bhutan",
				"Bolivia",
				"Bosnia/Herzegowina",
				"Botswana",
				"Bouvet Island",
				"Brasil",
				"British Ind. Oc. Ter.",
				"Brunei Darussalam",
				"Bulgaria",
				"Burkina Faso",
				"Burundi",
				"Cambodia",
				"Cameroon",
				"Canada",
				"Cape Verde",
				"Cayman Islands",
				"Central African Rep.",
				"Chad",
				"Chile",
				"China",
				"Christmas Island",
				"Cocos Islands",
				"Colombia",
				"Comoros",
				"Congo",
				"Cook Islands",
				"Costa Rica",
				"Cote D'ivoire",
				"Croatia",
				"Cuba",
				"Cyprus",
				"Czech Republic",
				"Denmark",
				"Djibouti",
				"Dominica",
				"Dominican Republic",
				"East Timor",
				"Ecuador",
				"Egypt",
				"El Salvador",
				"Equatorial Guinea",
				"Eritrea",
				"Estonia",
				"Ethiopia",
				"Falkland Islands",
				"Faroe Islands",
				"Fiji",
				"Finland",
				"France",
				"French Guiana",
				"French Polynesia",
				"French So. Ter.",
				"Gabon",
				"Gambia",
				"Georgia",
				"Germany",
				"Ghana",
				"Gibraltar",
				"Greece",
				"Greenland",
				"Grenada",
				"Guadeloupe",
				"Guam",
				"Guatemala",
				"Guinea",
				"Guinea-bissau",
				"Guyana",
				"Haiti",
				"Heard / McDonald Is.",
				"Honduras",
				"Hong Kong",
				"Hungary",
				"Iceland",
				"India",
				"Indonesia",
				"Iran ",
				"Iraq",
				"Ireland",
				"Israel",
				"Italy",
				"Jamaica",
				"Japan",
				"Jordan",
				"Kazakhstan",
				"Kenya",
				"Kiribati",
				"Korea (North)",
				"Korea (South)",
				"Kuwait",
				"Kyrgyzstan",
				"Laos",
				"Latvia",
				"Lebanon",
				"Lesotho",
				"Liberia",
				"Libya",
				"Liechtenstein",
				"Lithuania",
				"Luxembourg",
				"Macau",
				"Macedonia",
				"Madagascar",
				"Malawi",
				"Malaysia",
				"Maldives",
				"Mali",
				"Malta",
				"Marshall Islands",
				"Martinique",
				"Mauritania",
				"Mauritius",
				"Mayotte",
				"Mexico",
				"Micronesia",
				"Moldova",
				"Monaco",
				"Mongolia",
				"Montserrat",
				"Morocco",
				"Mozambique",
				"Myanmar",
				"Namibia",
				"Nauru",
				"Nepal",
				"Netherlands",
				"Netherlands Antilles",
				"New Caledonia",
				"New Zealand",
				"Nicaragua",
				"Niger",
				"Nigeria",
				"Niue",
				"Norfolk Island",
				"North Mariana Islands",
				"Norway",
				"Oman",
				"Pakistan",
				"Palau",
				"Panama",
				"Papua New Guinea",
				"Paraguay",
				"Peru",
				"Philippines",
				"Pitcairn",
				"Poland",
				"Portugal",
				"Puerto Rico",
				"Qatar",
				"Reunion",
				"Romania",
				"Russian Federation",
				"Rwanda",
				"Saint Kitts And Nevis",
				"Saint Lucia",
				"St. Vincent & Gren.",
				"Samoa",
				"Saudi Arabia",
				"Senegal",
				"Seychelles",
				"Sierra Leone",
				"Singapore",
				"Slovakia",
				"Slovenia",
				"Solomon Islands",
				"Somalia",
				"South Africa",
				"S. Georgia Sand. Is.",
				"Spain",
				"Sri Lanka",
				"St. Helena",
				"St. Pierre And Miquelon",
				"Sudan",
				"Suriname",
				"Svalbard Jan Mayen Isl.",
				"Swaziland",
				"Sweden",
				"Switzerland",
				"Syrian Arab Republic",
				"Taiwan",
				"Tajikistan",
				"Tanzania",
				"Thailand",
				"Togo",
				"Tokelau",
				"Tonga",
				"Trinidad And Tobago",
				"Tunisia",
				"Turkey",
				"Turkmenistan",
				"Turks/Caicos Islands",
				"Tuvalu",
				"Uganda",
				"Ukraine",
				"United Arab Emirates",
				"United Kingdom",
				"United States",
				"United States Islands",
				"Uruguay",
				"Uzbekistan",
				"Vanuatu",
				"Vatican City State",
				"Venezuela",
				"Viet Nam",
				"Virgin Islands (Brit)",
				"Virgin Islands (US)",
				"Wallis And Futuna Is.",
				"Western Sahara",
				"Yemen",
				"Yugoslavia",
				"Zaire",
				"Zambia",
				"Zimbabwe"
		};
		
		ret.put("pays",pays);
		return ret;
	}

	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object formulaire, BindException exceptions) throws Exception {
		
		Utilisateur utilisateur = AjouterUtilisateurFormulaire.newInstantanceUtilisateur((Utilisateur)formulaire);
		
		//effectuer l'enregistrement
		utilisateur = getServicePersonne().editerUtilisateur(utilisateur);
	
		
		Map model = new HashMap();
		model.put("unutilisateur",utilisateur);
		return new ModelAndView(getSuccessView(),model);
	}



	protected void onBindAndValidate(HttpServletRequest request, Object formulaire, BindException errors) throws Exception {
		//vérifier que le login n'est pas déjà utilisé
		AjouterUtilisateurFormulaire utilisateur = (AjouterUtilisateurFormulaire)formulaire;
		if(!getAncienUtilisateur().getLogin().equals(utilisateur.getLogin()))
		{
			Utilisateur u = servicePersonne.chercherUtilisateurParLogin(utilisateur.getLogin());
			if(u != null)
			{
				errors.rejectValue("login","AjouterUtilisateurFormulaire.login.dejautilise");
			}
		}
	}



	public Utilisateur getAncienUtilisateur() {
		return ancienUtilisateur;
	}



	public void setAncienUtilisateur(Utilisateur ancienUtilisateur) {
		this.ancienUtilisateur = ancienUtilisateur;
	}

}
